package com.hpf.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hpf.lease.common.config.CommonException;
import com.hpf.lease.common.result.ResultCodeEnum;
import com.hpf.lease.model.bean.FacilityInfo;
import com.hpf.lease.web.admin.mapper.FacilityInfoMapper;
import com.hpf.lease.web.admin.service.FacilityInfoService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;


/**
* @author GOD
* @description 针对表【facility_info(配套信息表)】的数据库操作Service实现
* @createDate 2024-08-29 15:00:50
*/
@Service
public class FacilityInfoServiceImpl extends ServiceImpl<FacilityInfoMapper, FacilityInfo>
    implements FacilityInfoService {
    @Resource
    FacilityInfoMapper facilityInfoMapper;
    @Override
    public boolean saveOrUpdateInfo(FacilityInfo facilityInfo) {
        Long id = facilityInfo.getId() == null ? -1L : facilityInfo.getId();
        FacilityInfo one = this.getOne(Wrappers.lambdaQuery(FacilityInfo.class)
                .eq(FacilityInfo::getName, facilityInfo.getName()));
        if (one != null && !one.getId().equals(id)){
            throw new CommonException(ResultCodeEnum.FACILITY_TYPE_EXISTS_ERROR);
        }
        return this.saveOrUpdate(facilityInfo);
    }

    @Override
    public List<FacilityInfo> listByType(Integer type) {
        LambdaQueryWrapper<FacilityInfo> wrapper = null;
        if (type != null){
            wrapper = Wrappers.lambdaQuery(FacilityInfo.class)
                    .eq(FacilityInfo::getType, type);
        }
        return this.list(wrapper);
    }
}




